package com.heima.behavior.service.impl;

import com.heima.behavior.dtos.FollowBehaviorDto;
import com.heima.behavior.pojos.ApFollowBehavior;
import com.heima.behavior.service.ApFollowBehaviorService;
import com.heima.model.common.apuser.pojos.ApUser;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.threadLocal.AppThreadLocal;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApFollowBehaviorServiceImpl implements ApFollowBehaviorService {


    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 关注用户
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult follow(FollowBehaviorDto dto) {
        //1.校验参数
        if (dto.getArticleId()==null || dto.getAuthorId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //1.5获取登陆人用户id
        ApUser apUser = AppThreadLocal.getApUser();
        //未登陆
        if(apUser==null){
            return ResponseResult.okResult(AppHttpCodeEnum.NEED_LOGIN);

        }
        //游客身份不能进行以下操作
        if(apUser.getId()==0){
            return ResponseResult.okResult(405,"游客身份，需进行登陆");

        }
        //用户是否已经关注
        String follow_key="USER_FOLLOW_"+apUser.getId();
        //定义关注key
        String fans_key="USER_FOLLOW_"+dto.getAuthorId();
        //判断hash中的大key中是小key中是否包含，如果包含，则返回true，否则false
        Boolean followKeyFlag = redisTemplate.opsForHash().hasKey(follow_key, dto.getAuthorId() + "");

        //2.1用户已经关注了，并且此时要执行关注操作operation=0,直接返回
        if (followKeyFlag && dto.getOperation()==0){
            return ResponseResult.okResult(AppHttpCodeEnum.FOLLOW_USER_END_SUCCESS);
        }
        //2.2用户已经关注，则此时是要执行取消关注操作，operation=1,删除redis和mongo
        if (followKeyFlag && dto.getOperation()==1){
            //2.2.1删除redis中两个hash
            redisTemplate.opsForHash().delete(fans_key,dto.getArticleId()+"");
            redisTemplate.opsForHash().delete(fans_key,dto.getAuthorId()+"");

            //2.2.2删除mongo中的数据
            Query query = Query.query(Criteria.where("userId").is(apUser.getId()).and("followId").is(dto.getAuthorId()));
            mongoTemplate.remove(query, ApFollowBehavior.class);
        }
        //2.3用户没有关注，并且此时执行的是关注操作operation=0,新增数据到redis和mongo中
        if(!followKeyFlag && dto.getOperation() ==0){
            //1.新增redis，两个hash
            redisTemplate.opsForHash().put(follow_key,dto.getAuthorId()+"","1");
            redisTemplate.opsForHash().put(fans_key,apUser.getId()+"","1");

            //2.新增mongo数据
            ApFollowBehavior apFollowBehavior = new ApFollowBehavior();
            apFollowBehavior.setId(ObjectId.get().toHexString());
            apFollowBehavior.setFollowId(dto.getAuthorId());
            apFollowBehavior.setUserId(Long.valueOf(apUser.getId()));
            apFollowBehavior.setArticleId(dto.getArticleId());
            apFollowBehavior.setCreatedTime(new Date());
            mongoTemplate.insert(apFollowBehavior);
        }

        return ResponseResult.okResult("关注用户成功");
    }
}
